package com.common;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;

public class CommonDriver {
    //单例模式
    private static ChromeDriver driver;
    private static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-hhmmssSS");
    private CommonDriver(){

    }
    public static ChromeDriver getDriver(){
        if (driver == null){
            driver = new ChromeDriver();
            //添加隐式等待
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        }
        return driver;
    }

    public static void fileStr(ChromeDriver driver){
        String format = CommonDriver.format.format(new Date());
        String root = "./src/test/screenShut/";
        //屏幕截图
        File file = new File(root+format+".png");
        File screenshot = driver.getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(screenshot,file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
